<?php
require_once './system/common.inc.php';
$path_info = $_SERVER['REQUEST_URI'];
if(!$path_info) exit('Access Denied');
$path_info = explode('/',$path_info);
$path_info['4'] = explode('.',$path_info['4']);
$file_uid = $path_info['3'];
$file_id = $path_info['4']['0'];
$file_type = $path_info['4']['1'];
$query = DB::query("SELECT * FROM `songs` WHERE `id`='{$file_id}'");
while($result = DB::fetch($query)) $song = $result;
if(!$song) exit('Access Denied');
if($song['uid']!=$file_uid) exit('This file is not belongs to you.');
if($file_type != 'mp3') exit('This file format does not supported to share.');
switch($song['fileserver_type']){
	case 'vdisk':
		$code = $song['fileserver_data'];
		if(!$code) exit('Access Denied');
		$opts = array(
			'http'=>array(
				'method'=>"GET",
				'header'=>"User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
			)
		);
		$context = stream_context_create($opts);
		$url="http://www.vdisk.cn/down/index/".$code;
		$data = file_get_contents($url,false,$context);
		preg_match("/name=.httpfileurl..content=.(.*?).>/", $data, $data);
		$myurl = $data[1];
		if($myurl){
			header('Content-Type:application/force-download');
			header("Location:".$myurl);
			die();
		}else exit('Access Denied');
		break;
	case 'baidupcs':
		$code = $song['fileserver_data'];
		$url = "http://pan.baidu.com/share/link".$code;
		$txes = file_get_contents($url);
		preg_match('/dlink(.*?)\"}";disk/i',$txes,$down);
		$quantity=count($down);
		if($quantity!="0"){
			preg_match('/\":(.*?)"}";disk/i',$down[0],$down1);
			$urlif=str_replace('\"','',$down1[1]);
			$urlif=str_replace('\\','',$urlif);
			header("Location:".$urlif);
		}else{
			exit('System Error:File not found in BaiduPCS');
		}
		break;
	case 'qiniu':
		exit('Unsupported');
		break;
	default:
		exit('Access Denied');
		break;
}